package com.kyxx.topics.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kyxx.topics.BaseApp;
import com.kyxx.topics.R;
import com.kyxx.topics.bean.AddQuanBean;
import com.kyxx.topics.bean.MemberAddCardBean;
import com.kyxx.topics.net.ResponseListener;
import com.kyxx.topics.net.RestClient;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.ToastUtils;
import com.kyxx.topics.view.CircleImageView;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2016/10/29.
 */
public class AddQuanAdapter extends RecyclerView.Adapter<AddQuanAdapter.ViewHolder> {
    private Context mContext;
    private List<AddQuanBean> mDatas;
    private LayoutInflater mInflater;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;

    public AddQuanAdapter(Context mContext, List<AddQuanBean> mDatas) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        this.mInflater = LayoutInflater.from(mContext);
        options = new DisplayImageOptions.Builder()
                // 设置图片在下载期间显示的图片
                .showImageOnLoading(R.mipmap.image_default)
                // 设置图片Uri为空或是错误的时候显示的图片
                .showImageForEmptyUri(R.mipmap.image_default)
                // 设置图片加载/解码过程中错误时候显示的图片
                .showImageOnFail(R.mipmap.image_default)
                .cacheInMemory(true)
                // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)
                // 设置下载的图片是否缓存在SD卡中
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型
                // .displayer(new RoundedBitmapDisplayer(9999))
                .resetViewBeforeLoading(false)
                .build();
    }

    @Override
    public AddQuanAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mInflater.inflate(R.layout.item_quan_add, parent, false));
    }

    @Override
    public void onBindViewHolder(final AddQuanAdapter.ViewHolder holder, final int position) {
        final AddQuanBean bean = mDatas.get(position);
        holder.tv_name.setText(bean.getTitle());
        imageLoader.displayImage(UrlUtils.BASEHTTP + bean.getImgurl(), holder.civ_huiyuanka, options);
        if (bean.getType() == 1) {//体验券
            holder.tv_count.setText("共可使用" + bean.getTimes() + "次");
        } else if (bean.getType() == 2) {//抵扣券
            holder.tv_count.setText("满" + bean.getJianmian() + "元  " + "抵扣" + bean.getDikou() + "元");
        }
        holder.tv_least.setText("剩余" + bean.getTotal() + "张");
        if (bean.getGet_limit()>bean.getHas_ticked()) {
            holder.btn.setEnabled(true);
            holder.btn.setText("领取");
        } else {
            holder.btn.setEnabled(false);
            holder.btn.setText("已领取");
        }
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("uid", BaseApp.getModel().getId());
                jsonObject.put("nickname", BaseApp.getModel().getNickname());
                jsonObject.put("coupon_id", mDatas.get(position).getId());
                jsonObject.put("num", "1");
                RequestParams params = new RequestParams();
                params.put("json", jsonObject.toString());
                RestClient.post(UrlUtils.TICKCOUPON, params, mContext, new ResponseListener(mContext, true) {
                    @Override
                    public void success(int arg0, Header[] arg1, org.json.JSONObject result) {
                        ToastUtils.show(mContext, "成功领取优惠券");
                        bean.setHas_ticked(1);
                        holder.btn.setEnabled(false);
                        holder.btn.setText("已领取");
                        bean.setTotal(Long.valueOf(bean.getTotal()) - 1 + "");
                        holder.tv_least.setText("剩余" + bean.getTotal() + "张");
                    }
                });
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView civ_huiyuanka;
        TextView tv_count;
        TextView btn;
        TextView tv_least;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            civ_huiyuanka = (ImageView) itemView.findViewById(R.id.civ_huiyuanka);
            tv_count = (TextView) itemView.findViewById(R.id.tv_count);
            btn = (Button) itemView.findViewById(R.id.btn);
            tv_least = (TextView) itemView.findViewById(R.id.tv_least);
        }
    }
}
